﻿using Neo.Compiler;
using Neo.Compiler.MSIL;
using Neo.Compiler.JVM;
using Neo.VM;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Windows.Forms;

namespace testtool
{
    public partial class Form1 : Form, ILogger
    {
        public Form1()
        {
            InitializeComponent();
        }

        ILModule moduleIL;
        ILModule moduleIL_vb;
        JavaModule moduleJVMPackage;
        JavaModule moduleJVMPackage_kt;

        //JavaClass moduleJVM;
        Neo.Compiler.MSIL.ModuleConverter converterIL;
        Neo.Compiler.MSIL.ModuleConverter converterIL_vb;
        Neo.Compiler.JVM.ModuleConverter converterJVM;
        Neo.Compiler.JVM.ModuleConverter converterJVM_kt;
        private void Form1_Load(object sender, EventArgs e)
        {
            {
                //一个快速使用的方法
                //byte[] code= AntShares.VM.ILConverter.Converter.Convert(dll);

                //load test dll
                var dll = System.IO.File.OpenRead("testlib.dll");
                var dllpdb = System.IO.File.OpenRead("testlib.pdb");
                this.moduleIL = new ILModule();
                //不需要pdb 传个null 就行，只是不会提示代码关联，没别的影响
                //this.module.LoadModule(dll, null);
                this.moduleIL.LoadModule(dll, dllpdb);
                dll.Close();
                dllpdb.Close();

                //转换为小蚁vmcode
                this.converterIL = new Neo.Compiler.MSIL.ModuleConverter(this);

                Neo.Compiler.ConvOption op = new ConvOption();
                op.useNep8 = true;
                var am = this.converterIL.Convert(this.moduleIL,op);
                var bs = am.Build();
                var json = vmtool.FuncExport.Export(am, bs);
                StringBuilder sb = new StringBuilder();
                json.ConvertToStringWithFormat(sb, 0);
                System.IO.File.WriteAllText("csharp.json", sb.ToString());
            }
            {
                //一个快速使用的方法
                //byte[] code= AntShares.VM.ILConverter.Converter.Convert(dll);

                //load test dll
                var dll = System.IO.File.OpenRead("testlib_vb.dll");
                var dllpdb = System.IO.File.OpenRead("testlib_vb.pdb");
                this.moduleIL_vb = new ILModule();
                //不需要pdb 传个null 就行，只是不会提示代码关联，没别的影响
                //this.module.LoadModule(dll, null);
                this.moduleIL_vb.LoadModule(dll, dllpdb);
                dll.Close();
                dllpdb.Close();

                //转换为小蚁vmcode
                this.converterIL_vb = new Neo.Compiler.MSIL.ModuleConverter(this);
                var am = this.converterIL_vb.Convert(this.moduleIL_vb);
                var bs = am.Build();
                var json = vmtool.FuncExport.Export(am, bs);
                StringBuilder sb = new StringBuilder();
                json.ConvertToStringWithFormat(sb, 0);
                System.IO.File.WriteAllText("vb.json", sb.ToString());

            }
            //JVM
            {
                this.moduleJVMPackage = new JavaModule();
                this.moduleJVMPackage.LoadClass("go.class", "");
                this.moduleJVMPackage.LoadJar("neo.SmartContract.Framework.jar", "");

                //var dll = System.IO.File.ReadAllBytes("go.class");

                //var js = new javaloader.ClassFile(dll, 0, dll.Length);
                //var src = System.IO.File.ReadAllLines("go.java");
                //this.moduleJVM = new JavaClass(js, src);

                //转换为小蚁vmcode
                this.converterJVM = new Neo.Compiler.JVM.ModuleConverter(this);
                this.converterJVM.Convert(this.moduleJVMPackage);
            }
            //kotlin
            {
                this.moduleJVMPackage_kt = new JavaModule();
                this.moduleJVMPackage_kt.LoadClass("go_kt.class", "");
                this.moduleJVMPackage_kt.LoadJar("neo.SmartContract.Framework.jar", "");

                //var dll = System.IO.File.ReadAllBytes("go.class");

                //var js = new javaloader.ClassFile(dll, 0, dll.Length);
                //var src = System.IO.File.ReadAllLines("go.java");
                //this.moduleJVM = new JavaClass(js, src);

                //转换为小蚁vmcode
                this.converterJVM_kt = new Neo.Compiler.JVM.ModuleConverter(this);
                this.converterJVM_kt.Convert(this.moduleJVMPackage_kt);

            }
            this.richTextBox1.Text = this.converterIL.outModule.GenJson();
            //System.IO.File.WriteAllText("testout.json", this.richTextBox1.Text);
            initTreeData();
        }

        private void initTreeData()
        {
            treeData.Nodes.Clear();
            //testdata
            //TreeNode root = new TreeNode("testdata");
            //treeData.Nodes.Add(root);

            //string[] files = System.IO.Directory.GetFiles("testdata", "*.txt");
            //foreach (var f in files)
            //{
            //    TreeNode sn = new TreeNode(System.IO.Path.GetFileName(f));
            //    sn.Tag = new CodeFile(f);
            //    root.Nodes.Add(sn);
            //}
            //ildata
            {
                TreeNode root = new TreeNode("from testlib.dll");
                root.Tag = converterIL;
                treeData.Nodes.Add(root);
                foreach (var t in this.moduleIL.mapType)
                {
                    if (t.Key[0] == '<') continue;//系统的，不要
                    if (t.Key.Contains("_API_")) continue;//api的，不要
                    TreeNode sn = new TreeNode(t.Key);
                    sn.Tag = t.Value;
                    root.Nodes.Add(sn);
                    //fields
                    foreach (var f in t.Value.fields)
                    {
                        TreeNode ssn = new TreeNode(f.Key);
                        ssn.Tag = f.Value;
                        sn.Nodes.Add(ssn);
                    }
                    //methods
                    foreach (var m in t.Value.methods)
                    {
                        if (m.Value.method == null) continue;
                        var pname = "()";
                        if (m.Value.hasParam)
                        {
                            pname = "(..." + m.Value.paramtypes.Count + "ps)";
                        }
                        TreeNode ssn = new TreeNode(m.Key + pname);
                        ssn.Tag = m.Value;
                        sn.Nodes.Add(ssn);
                    }
                }
                if (converterIL.methodLink.ContainsValue(converterIL.outModule.mapMethods[converterIL.outModule.mainMethod]) == false)
                {//入口函数是虚拟的
                    TreeNode ssn = new TreeNode("跳转入口");
                    var spec = new specILMethod()
                    {
                        _method = converterIL.outModule.mapMethods[converterIL.outModule.mainMethod]
                    };
                    ssn.Tag = spec;
                    root.Nodes.Add(ssn);
                }

            }
            {
                TreeNode root = new TreeNode("from testlib_vb.dll");
                root.Tag = converterIL_vb;
                treeData.Nodes.Add(root);
                foreach (var t in this.moduleIL_vb.mapType)
                {
                    if (t.Key[0] == '<') continue;//系统的，不要
                    if (t.Key.Contains("_API_")) continue;//api的，不要
                    TreeNode sn = new TreeNode(t.Key);
                    sn.Tag = t.Value;
                    root.Nodes.Add(sn);
                    //fields
                    foreach (var f in t.Value.fields)
                    {
                        TreeNode ssn = new TreeNode(f.Key);
                        ssn.Tag = f.Value;
                        sn.Nodes.Add(ssn);
                    }
                    //methods
                    foreach (var m in t.Value.methods)
                    {
                        if (m.Value.method == null) continue;
                        var pname = "()";
                        if (m.Value.hasParam)
                        {
                            pname = "(..." + m.Value.paramtypes.Count + "ps)";
                        }
                        TreeNode ssn = new TreeNode(m.Key + pname);
                        ssn.Tag = m.Value;
                        sn.Nodes.Add(ssn);
                    }
                }
            }
            //JVM
            {
                TreeNode root = new TreeNode("from go.class");
                root.Tag = this.converterJVM;
                treeData.Nodes.Add(root);

                foreach (JavaClass c in this.moduleJVMPackage.classes.Values)
                {
                    if (c.skip) continue;
                    TreeNode sn = new TreeNode(c.classfile.Name);
                    sn.Tag = c;
                    root.Nodes.Add(sn);
                    //fields
                    foreach (var f in c.fields)
                    {
                        TreeNode ssn = new TreeNode(f.Key);
                        ssn.Tag = f.Value;
                        sn.Nodes.Add(ssn);
                    }
                    //methods
                    foreach (var m in c.methods)
                    {
                        if (m.Value.skip) continue;
                        if (m.Value.method == null) continue;
                        var pname = "()";
                        if (m.Value.paramTypes.Count > 0)
                        {
                            pname = "(..." + m.Value.paramTypes.Count + "ps)";
                        }
                        TreeNode ssn = new TreeNode(m.Key + pname);
                        ssn.Tag = m.Value;
                        sn.Nodes.Add(ssn);
                    }
                }

            }
            //JVM——kt
            {
                TreeNode root = new TreeNode("from go.class");
                root.Tag = this.converterJVM_kt;
                treeData.Nodes.Add(root);

                foreach (JavaClass c in this.moduleJVMPackage_kt.classes.Values)
                {
                    if (c.skip) continue;
                    TreeNode sn = new TreeNode(c.classfile.Name);
                    sn.Tag = c;
                    root.Nodes.Add(sn);
                    //fields
                    foreach (var f in c.fields)
                    {
                        TreeNode ssn = new TreeNode(f.Key);
                        ssn.Tag = f.Value;
                        sn.Nodes.Add(ssn);
                    }
                    //methods
                    foreach (var m in c.methods)
                    {
                        if (m.Value.skip) continue;
                        if (m.Value.method == null) continue;
                        var pname = "()";
                        if (m.Value.paramTypes.Count > 0)
                        {
                            pname = "(..." + m.Value.paramTypes.Count + "ps)";
                        }
                        TreeNode ssn = new TreeNode(m.Key + pname);
                        ssn.Tag = m.Value;
                        sn.Nodes.Add(ssn);
                    }
                }

            }
            treeData.ExpandAll();
        }

        private void treeData_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node != null)
            {
                //finfo = e.Node.Tag as CodeFile;
                var methodIL = e.Node.Tag as ILMethod;
                var methodJVM = e.Node.Tag as JavaMethod;

                if (methodIL != null)
                {
                    if (this.converterIL.methodLink.ContainsKey(methodIL) || methodIL is specILMethod)
                        setMethod(methodIL);
                    else if (this.converterIL_vb.methodLink.ContainsKey(methodIL))
                        setMethod_vb(methodIL);
                }
                else if (methodJVM != null)
                {
                    if (this.converterJVM.methodLink.ContainsKey(methodJVM))
                        setMethod(methodJVM);
                    else if (this.converterJVM_kt.methodLink.ContainsKey(methodJVM))
                        setMethod_kt(methodJVM);
                }

            }
        }
        NeoMethod curCode;
        //CodeClip curCode;
        //private void setFile(CodeFile info)
        //{
        //    if (info == null)
        //    {
        //        this.textBytes.Text = "";
        //        this.listOpCode.Items.Clear();
        //        curCode = null;
        //    }
        //    else
        //    {
        //        var code = info.GetCode();
        //        curCode = new AntShares.VM.Tool.CodeClip();
        //        curCode.Parse(code);
        //        this.textBytes.Text = CodeFile.FormatCode(code);
        //        this.listOpCode.Items.Clear();
        //        foreach (var c in curCode.opcodes)
        //        {
        //            this.listOpCode.Items.Add(c);
        //        }
        //    }
        //}
        Dictionary<int, int> cacheLine = new Dictionary<int, int>();
        string tag = "";
        public class specILMethod : ILMethod
        {
            public specILMethod() : base(null, null)
            {

            }
            public NeoMethod _method;
        }

        private void setMethod(ILMethod method)
        {
            this.richTextBox1.Text = this.converterIL.outModule.GenJson();
            tag = "IL";
            if (method == null)
            {
                this.listILCode.Items.Clear();
                this.listOpCode.Items.Clear();
            }
            else
            {
                this.listILCode.Items.Clear();
                this.listOpCode.Items.Clear();
                specILMethod spec = method as specILMethod;
                NeoMethod am = null;
                if (spec == null)
                {
                    //临时变量
                    this.listILCode.Items.Add("==Variables==");
                    foreach (var c in method.body_Variables)
                    {
                        this.listILCode.Items.Add(c);
                    }
                    this.listILCode.Items.Add("==body==" + "(下面的括号里面是代码行号)");

                    foreach (var c in method.body_Codes.Values)
                    {
                        this.listILCode.Items.Add(c);
                    }
                    am = this.converterIL.methodLink[method];
                }
                else
                {
                    am = spec._method;
                }
                //opcode
                this.listOpCode.Items.Add("==Variables==");
                foreach (var c in am.body_Variables)
                {
                    this.listOpCode.Items.Add(c);
                }
                this.listOpCode.Items.Add("func=" + am.name);
                this.listOpCode.Items.Add("addr=" + am.funcaddr.ToString("X04"));

                this.curCode = am;


                //跳至代码行数
                richTextBox1.SelectionProtected = true;
                var indexpos = richTextBox1.Find("\"" + am.funcaddr.ToString("X04"));
                var linepos = richTextBox1.GetLineFromCharIndex(indexpos);
                var line = richTextBox1.Lines[linepos];
                var b = richTextBox1.GetFirstCharIndexFromLine(linepos);
                richTextBox1.Select(b, line.Length);
                richTextBox1.ScrollToCaret();

            }
        }
        private void setMethod_vb(ILMethod method)
        {
            this.richTextBox1.Text = this.converterIL_vb.outModule.GenJson();
            tag = "IL_vb";
            if (method == null)
            {
                this.listILCode.Items.Clear();
                this.listOpCode.Items.Clear();
            }
            else
            {
                this.listILCode.Items.Clear();
                this.listOpCode.Items.Clear();
                //临时变量
                this.listILCode.Items.Add("==Variables==");
                foreach (var c in method.body_Variables)
                {
                    this.listILCode.Items.Add(c);
                }
                this.listILCode.Items.Add("==body==" + "(下面的括号里面是代码行号)");

                foreach (var c in method.body_Codes.Values)
                {
                    this.listILCode.Items.Add(c);
                }

                //opcode
                var am = this.converterIL_vb.methodLink[method];
                this.listOpCode.Items.Add("==Variables==");
                foreach (var c in am.body_Variables)
                {
                    this.listOpCode.Items.Add(c);
                }
                this.listOpCode.Items.Add("func=" + am.name);
                this.listOpCode.Items.Add("addr=" + am.funcaddr.ToString("X04"));

                this.curCode = am;


                //跳至代码行数
                richTextBox1.SelectionProtected = true;
                var indexpos = richTextBox1.Find("\"" + am.funcaddr.ToString("X04"));
                var linepos = richTextBox1.GetLineFromCharIndex(indexpos);
                var line = richTextBox1.Lines[linepos];
                var b = richTextBox1.GetFirstCharIndexFromLine(linepos);
                richTextBox1.Select(b, line.Length);
                richTextBox1.ScrollToCaret();

            }
        }
        private void setMethod(JavaMethod method)
        {
            this.richTextBox1.Text = this.converterJVM.outModule.GenJson();
            tag = "JVM";
            if (method == null || this.converterJVM.methodLink.ContainsKey(method) == false)
            {
                this.listILCode.Items.Clear();
                this.listOpCode.Items.Clear();
            }
            else
            {
                this.listILCode.Items.Clear();
                this.listOpCode.Items.Clear();
                //临时变量
                this.listILCode.Items.Add("==Variables==");
                //foreach (var a in method.method.LocalVariableTableAttribute)
                //{
                //    this.listILCode.Items.Add(a.name + " " + a.index + " " + a.start_pc + " " + a.descriptor);
                //}
                //this.listILCode.Items.Add(method.addLocal_VariablesCount);
                foreach (var c in method.body_Variables)
                {
                    this.listILCode.Items.Add(c);
                }
                this.listILCode.Items.Add("==body==" + "(下面的括号里面是代码行号)");

                foreach (var c in method.body_Codes.Values)
                {
                    this.listILCode.Items.Add(c);
                }

                //opcode
                var am = this.converterJVM.methodLink[method];
                this.listOpCode.Items.Add("==Variables==");
                //this.listOpCode.Items.Add(am.addVariablesCount);
                foreach (var c in am.body_Variables)
                {
                    this.listOpCode.Items.Add(c);
                }
                this.listOpCode.Items.Add("func=" + am.name);
                this.listOpCode.Items.Add("addr=" + am.funcaddr.ToString("X04"));

                this.curCode = am;


                //跳至代码行数
                richTextBox1.SelectionProtected = true;
                var indexpos = richTextBox1.Find("\"" + am.funcaddr.ToString("X04"));
                var linepos = richTextBox1.GetLineFromCharIndex(indexpos);
                var line = richTextBox1.Lines[linepos];
                var b = richTextBox1.GetFirstCharIndexFromLine(linepos);
                richTextBox1.Select(b, line.Length);
                richTextBox1.ScrollToCaret();

            }
        }
        private void setMethod_kt(JavaMethod method)
        {
            this.richTextBox1.Text = this.converterJVM_kt.outModule.GenJson();
            tag = "JVM_kt";
            if (method == null || this.converterJVM_kt.methodLink.ContainsKey(method) == false)
            {
                this.listILCode.Items.Clear();
                this.listOpCode.Items.Clear();
            }
            else
            {
                this.listILCode.Items.Clear();
                this.listOpCode.Items.Clear();
                //临时变量
                this.listILCode.Items.Add("==Variables==");
                //foreach (var a in method.method.LocalVariableTableAttribute)
                //{
                //    this.listILCode.Items.Add(a.name + " " + a.index + " " + a.start_pc + " " + a.descriptor);
                //}
                //this.listILCode.Items.Add(method.addLocal_VariablesCount);
                foreach (var c in method.body_Variables)
                {
                    this.listILCode.Items.Add(c);
                }
                this.listILCode.Items.Add("==body==" + "(下面的括号里面是代码行号)");

                foreach (var c in method.body_Codes.Values)
                {
                    this.listILCode.Items.Add(c);
                }

                //opcode
                var am = this.converterJVM_kt.methodLink[method];
                this.listOpCode.Items.Add("==Variables==");
                //this.listOpCode.Items.Add(am.addVariablesCount);
                foreach (var c in am.body_Variables)
                {
                    this.listOpCode.Items.Add(c);
                }
                this.listOpCode.Items.Add("func=" + am.name);
                this.listOpCode.Items.Add("addr=" + am.funcaddr.ToString("X04"));

                this.curCode = am;


                //跳至代码行数
                richTextBox1.SelectionProtected = true;
                var indexpos = richTextBox1.Find("\"" + am.funcaddr.ToString("X04"));
                var linepos = richTextBox1.GetLineFromCharIndex(indexpos);
                var line = richTextBox1.Lines[linepos];
                var b = richTextBox1.GetFirstCharIndexFromLine(linepos);
                richTextBox1.Select(b, line.Length);
                richTextBox1.ScrollToCaret();

            }
        }

        class container : IScriptContainer
        {
            public byte[] GetMessage()
            {
                return new byte[] { 1, 2, 3, 4 };
            }

            public byte[] ToArray()
            {
                throw new NotImplementedException();
            }
        }
        class crypto : ICrypto
        {
            public byte[] Hash160(byte[] message)
            {
                return new byte[] { 4, 56, 66, 5 };
            }

            public byte[] Hash256(byte[] message)
            {
                throw new NotImplementedException();
            }

            public bool VerifySignature(byte[] message, byte[] signature, byte[] pubkey)
            {
                return true;
            }
        }
        class table : Neo.VM.IScriptTable
        {
            public byte[] GetScript(byte[] script_hash)
            {
                if (script_hash.Length == 1 && script_hash[0] == 99)
                {
                    return new byte[] { (byte)Neo.VM.OpCode.DROP, (byte)Neo.VM.OpCode.DROP, (byte)Neo.VM.OpCode.RET };

                }
                return new byte[] { (byte)Neo.VM.OpCode.PUSH1, (byte)Neo.VM.OpCode.RET };
            }
        }
        ExecutionEngine getEngine()
        {
            var engine = new ExecutionEngine(new Neo.vmext.Transaction(), new crypto(), new table(), new Neo.vmext.testapiservice());
            this.engine = engine;
            return engine;
        }
        ExecutionEngine engine;

        private void button1_Click(object sender, EventArgs e)
        {//run button
            if (curCode == null)
                return;
            if (this.engine == null)
            {
                this.clearLog();
                this.engine = getEngine();




                if (this.tag == "IL")
                {
                    this.engine.LoadScript(this.converterIL.outModule.Build());
                }
                else if (this.tag == "IL_vb")
                {
                    this.engine.LoadScript(this.converterIL_vb.outModule.Build());
                }
                else if (this.tag == "JVM")
                {
                    this.engine.LoadScript(this.converterJVM.outModule.Build());

                }
                else if (this.tag == "JVM_kt")
                {
                    this.engine.LoadScript(this.converterJVM_kt.outModule.Build());

                }
                this.engine.InvocationStack.Peek().InstructionPointer = curCode.funcaddr;

                {
                    for (var i = 0; i < curCode.paramtypes.Count; i++)
                    {
                        var bn = new BigInteger(i * 100 + 99);
                        this.engine.CurrentContext.EvaluationStack.Push(bn);
                    }
                }
                this.Log("Start Engine:" + this.engine.State);
            }
            while (this.engine != null)
            {
                OneStep();
            }
        }

        void clearLog()
        {
            this.listConsole1.Items.Clear();
        }
        public void Log(string str)
        {
            this.listConsole1.Items.Add(str);
            this.listConsole1.SelectedIndex = this.listConsole1.Items.Count - 1;
        }

        private void button2_Click(object sender, EventArgs e)
        {//one step
            if (curCode == null)
                return;
            if (this.engine == null)
            {
                this.clearLog();
                this.engine = getEngine();
                var pcount = curCode.paramtypes.Count;
                if (curCode.name.Contains("@"))
                    pcount = 10;
                for (var i = 0; i < pcount; i++)
                {
                    var bn = new BigInteger(i * 100 + 99);
                    this.engine.CurrentContext.EvaluationStack.Push(bn);
                }

                if (this.tag == "IL")
                {
                    this.engine.LoadScript(this.converterIL.outModule.Build());
                }
                else if (this.tag == "IL_vb")
                {
                    this.engine.LoadScript(this.converterIL_vb.outModule.Build());
                }
                else if (this.tag == "JVM")
                {
                    this.engine.LoadScript(this.converterJVM.outModule.Build());
                }
                else if (this.tag == "JVM_kt")
                {
                    this.engine.LoadScript(this.converterJVM_kt.outModule.Build());

                }
                this.engine.InvocationStack.Peek().InstructionPointer = curCode.funcaddr;
                this.Log("Start Engine:" + this.engine.State);
            }

            OneStep();

        }
        void FillArray(System.Text.StringBuilder sb, StackItem v)
        {
            if (v is Neo.VM.Types.Array)
            {
                sb.Append("[");

                var vv = v as Neo.VM.Types.Array;
                for (var i = 0; i < vv.Count; i++)
                {
                    if (i != 0)
                        sb.Append(",");
                    FillArray(sb, vv[i]);

                }

                sb.Append("]");


            }
            else
            {
                try
                {
                    BigInteger iniv = v.GetBigInteger();
                    sb.Append(iniv.ToString());
                }
                catch
                {
                    sb.Append("?");
                }
            }
        }
        private void OneStep()
        {
            ExecutionContext context = engine.InvocationStack.Peek();

            this.Log("exe:(" + context.InstructionPointer.ToString("X02") + ")");

            var code = this.engine.CurrentContext.NextInstruction;
            this.engine.StepInto();

            if (this.engine.InvocationStack.Count > 0)
            {
                this.listValues.Items.Clear();
                listValuesAlt.Items.Clear();

                this.Log("    " + code.ToString() + "   - " + this.engine.State);

                foreach (StackItem v in engine.CurrentContext.EvaluationStack)
                {
                    if (v is Neo.VM.Types.Array)
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.Append("in stack:");
                        FillArray(sb, v);
                        this.listValues.Items.Add(sb.ToString());
                    }
                    else
                    {

                        try
                        {

                            BigInteger iniv = v.GetBigInteger();
                            string outstr = "in stack:" + iniv.ToString() + "(0X" + iniv.ToString("X02") + ")";
                            try
                            {
                                var bts = v.GetByteArray();
                                if (bts != null)
                                {
                                    for (var i = 0; i < bts.Length; i++)
                                    {
                                        outstr += bts[i].ToString("X02");
                                    }
                                }
                            }
                            catch
                            {

                            }
                            this.listValues.Items.Add(outstr);
                        }
                        catch
                        {
                            this.listValues.Items.Add("in stack:not number");

                        }
                    }
                }
                foreach (var v in engine.CurrentContext.AltStack)
                {
                    if (v is Neo.VM.Types.Array)
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.Append("in stack:");
                        FillArray(sb, v);
                        this.listValuesAlt.Items.Add(sb.ToString());
                    }
                    else
                    {
                        try
                        {
                            BigInteger iniv = v.GetBigInteger();
                            this.listValuesAlt.Items.Add("in stack:" + iniv.ToString() + "(0X" + iniv.ToString("X02") + ")");
                        }
                        catch
                        {
                            this.listValuesAlt.Items.Add("in stack:not number");

                        }
                    }
                }
            }
            if ((this.engine.State & VMState.FAULT) > 0)
            {
                this.Log("Engine FAULT End");
                this.engine = null;
                return;
            }
            if ((this.engine.State & VMState.HALT) > 0)
            {
                this.Log("Engine End");
                this.engine = null;
                return;
            }
        }

        private void textSrc_TextChanged(object sender, EventArgs e)
        {
            var src = textSrc.Text;
            var bytes = System.Text.Encoding.ASCII.GetBytes(src);
            string o = "";
            for (var i = 0; i < bytes.Length; i++)
            {
                o += bytes[i].ToString("X02");
            }
            textAscii.Text = o;

            textLength.Text = bytes.Length.ToString("X");
        }
    }
}
